#include "StdAfx.h"
#include "SplineBuilder.h"
#include "varlist.h"
#include "LabelTools.h"
#include "ShapeInfoFactory.h"

#include <TColgp_HArray1OfPnt.hxx>
#include <GeomAPI_Interpolate.hxx>
#include <Geom_BSplineCurve.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx> 



REGISTERE_SHAPEINFO(CSplineInfo, SPLINE_CUID);
//////////////////////////////////////////////////////////////////////////
CSplineInfo::CSplineInfo()
{
	m_szGUID = SPLINE_CUID;
	Reset();
}
CSplineInfo::~CSplineInfo()
{
}
void CSplineInfo::Reset()
{
	nPointCount = 0;
	ptStart = ptEnd = gp_Pnt(0,0,0);
}
void CSplineInfo::GetParams(CVarList& varList, UINT nID)
{
	varList.SetID(SPLINE_CUID);
	varList.SetValue(START_POINT, ptStart);
	varList.SetValue(END_POINT, ptEnd);
	varList.SetValue(POINT_COUNT, nPointCount);
}
void CSplineInfo::SetParams(const CVarList& varList, UINT nID)
{
	//CPointProxy point;
	//varList.GetValue(START_POINT, point);
	//ptStart = point;
	//varList.GetValue(END_POINT, point);
	//ptEnd = point;
	
}
bool CSplineInfo::GetFromLabel(TDF_Label& label)
{
	TDF_Label childLabel;

	if(!LabelTools::GetChild(label, START_POINT, ptStart, childLabel))
		return false;

	if(!LabelTools::GetChild(label, END_POINT, ptEnd, childLabel))
		return false;

	if(!LabelTools::GetChild(label, POINT_COUNT, nPointCount, childLabel))
		return false;

	if(!LabelTools::GetChild(label, POINT_ARRAY, hPoints, childLabel, nPointCount))
		return false;

	return true;
}

void CSplineInfo::AddToLabel(TDF_Label& label)
{
	nPointCount = hPoints->Upper();

	LabelTools::SetChild(label, START_POINT, ptStart);
	LabelTools::SetChild(label, END_POINT, ptEnd);
	LabelTools::SetChild(label, POINT_COUNT, nPointCount);
	LabelTools::SetChild(label, POINT_ARRAY, hPoints);
}

bool CSplineInfo::ModifyLabel(TDF_Label& label, TFunction_Logbook& log)
{
	if(!LabelTools::UpdateChild(label, START_POINT, ptStart, log))
		return false;

	if(!LabelTools::UpdateChild(label, END_POINT, ptEnd, log))
		return false;

	if(!LabelTools::UpdateChild(label, POINT_COUNT, nPointCount, log))
		return false;

	//if(!LabelTools::UpdateChild(label, POINT_ARRAY, hPoints, log))
	//	return false;
	return true;
}

bool CSplineInfo::IsModified(TDF_Label& label,const TFunction_Logbook& log)
{
	if(log.IsModified(label.FindChild(START_POINT)))
		return true; 

	if(log.IsModified(label.FindChild(END_POINT)))
		return true; 

	if(log.IsModified(label.FindChild(POINT_COUNT)))
		return true; 

	if(log.IsModified(label.FindChild(POINT_ARRAY)))
		return true; 


	return false;
}
//////////////////////////////////////////////////////////////////////////

CSplineBuilder::CSplineBuilder(void)
{
}

CSplineBuilder::~CSplineBuilder(void)
{
}

bool CSplineBuilder::Build(const CString& szCmdParam, TopoDS_Shape& shape)
{
	return false;
}

bool CSplineBuilder::Build(CSplineInfo* pShapeInfo, TopoDS_Shape& shape)
{
	try
	{
		if( pShapeInfo->hPoints->Upper()<2)
			return false;

		GeomAPI_Interpolate Interpolate(pShapeInfo->hPoints, Standard_False, 0);
		Interpolate.Perform();
		Handle(Geom_BSplineCurve) spline = Interpolate.Curve(); 

		BRepBuilderAPI_MakeEdge builder(spline);
		shape = builder.Shape();
	}
	catch(...)
	{
		return false;
	}

	return true;
}